<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" scope="page" value="${request.contextPath}" />
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE  >
<html >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,viewport-fit=cover">
    <title>坐席</title>
    <link rel="stylesheet" type="text/css" href="${ctx}/static/weui/1.1.2/weui-for-work.min.css"/>
    <script type="text/javascript" src="${ctx}/static/jquery/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="${ctx}/static/weui/1.1.2/weui.min.js"></script>
    <!-- 企业微信的JS-SDK -->
    <%--<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>--%>

</head>
<body style="background-color: #f8f8f8" >
<div class="page">
    <div class="page__bd">
        <input type="hidden" name="userId" id="userId" value="${userId}"/>
        <c:forEach items="${repaireInfos}" var="repaireInfo">
            <div class="weui-form-preview">
                <div class="weui-form-preview__bd">
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">报修人</label>
                        <span class="weui-form-preview__value"><c:out value="${repaireInfo.creatorName}"/></span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">公司名</label>
                        <span class="weui-form-preview__value"><c:out value="${repaireInfo.callerCompanyName}"/></span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">部门</label>
                        <span class="weui-form-preview__value"><c:out value="${repaireInfo.callerDeptName}"/></span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">地址</label>
                        <span class="weui-form-preview__value"><c:out value="${repaireInfo.address}"/></span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">选项</label>
                        <span class="weui-form-preview__value"><c:out value="${repaireInfo.machineType}"/></span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">判断常用</label>
                        <span class="weui-form-preview__value"><c:out value="${repaireInfo.repaireSubject}"/></span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">备注</label>
                        <span class="weui-form-preview__value"><c:out value="${repaireInfo.description}"/></span>
                    </div>
                    <div class="weui-form-preview__item">
                        <label class="weui-form-preview__label">创建时间</label>
                        <span class="weui-form-preview__value"><fmt:formatDate pattern="yyyy-MM-dd HH:mm" value="${repaireInfo.createTime}" type="both" /></span>
                    </div>
                    <c:if test="${repaireInfo.status=='已接单'}">
                        <div class="weui-form-preview__item">
                            <label class="weui-form-preview__label">服务时间</label>
                            <span class="weui-form-preview__value"><c:out value="${repaireInfo.expectTime}"/></span>
                        </div>
                    </c:if>
                </div>
                <div class="weui-form-preview__ft">
                    <c:if test="${(repaireInfo.status=='待接单' || repaireInfo.status=='待调配')}">
                        <a class="weui-form-preview__btn weui-form-preview__btn_default" href="javascript:acceptRepaire('${repaireInfo.id}','${repaireInfo.createTime}')">接单</a>
                        <button type="submit" class="weui-form-preview__btn weui-form-preview__btn_primary" onclick="javascript:allocationEngineer(${repaireInfo.id})">转单</button>

                    </c:if>
                    <c:if test="${repaireInfo.status=='已接单'}">
                        <a class="weui-form-preview__btn weui-form-preview__btn_default" href="javascript:beginServer(${repaireInfo.id})">开始服务</a>
                        <button type="submit" class="weui-form-preview__btn weui-form-preview__btn_primary" onclick="javascript:endServer(${repaireInfo.id})">结束服务</button>
                    </c:if>
                    <c:if test="${repaireInfo.status=='服务中'}">
                        <a class="weui-form-preview__btn weui-form-preview__btn_primary" href="javascript:endServer(${repaireInfo.id})">结束服务</a>
                    </c:if>
                </div>
            </div>
            <br>
        </c:forEach>
    </div>
    <c:if test="${empty repaireInfos}">

        <div class="weui-msg">
            <div class="weui-msg__icon-area"><i class="weui-icon-waiting weui-icon_msg"></i></div>
            <div class="weui-msg__text-area">
                <h2 class="weui-msg__title">暂时没有工单了</h2>
            </div>
        </div>
    </c:if>
</div>
<div class="page__bd" >
    <div class="weui-tab">
        <div class="weui-tabbar">
            <a href="${ctx}/weixin/customer/to_forward?code=&state=list" class="weui-tabbar__item weui-bar__item_on">
                <img src="${ctx}/static/images/icon_tabbar.png" alt="" class="weui-tabbar__icon">
                <p class="weui-tabbar__label">工单池</p>
            </a>
        </div>
    </div>
</div>

<div id="toast" style="display: none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-icon-warn weui-icon_toast"></i>
        <p class="weui-toast__content"></p>
    </div>
</div>

<div id="toast_success" style="display: none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-icon-success-no-circle weui-icon_toast"></i>
        <p class="weui-toast__content"></p>
    </div>
</div>
<script type="text/javascript" src="${ctx}/static/weixin/allocation_repaire.js" ></script>
<script type="text/javascript">
    /*function allocationEngineer(id){
        getEngineer(id,'${ctx}/weixin/allocation/to_allocation');
    }*/

    $(function () {
        var needRefresh = sessionStorage.getItem("need-refresh");
        if(needRefresh){
            sessionStorage.removeItem("need-refresh");
            location.reload();
        }
    });

    function dateToGMT(strDate){
        var dateStr=strDate.split(" ");
        var strGMT = dateStr[0]+" "+dateStr[1]+" "+dateStr[2]+" "+dateStr[5]+" "+dateStr[3]+" GMT+0800";
        var date = new Date(Date.parse(strGMT));
        return date;
    }
    var ctx = "${ctx}";
    function acceptRepaire(id,createTime) {
        createTime = dateToGMT(createTime);
        var date = new Date();
        date.setHours(17);
        date.setMinutes(30);
        var begin_times = [],
            end_times = [],
            symbol = [{ label: '-', value: 0 }];
        if(date.getDate() == createTime.getDate()){
            // 晚于17:30  1小时工作量
            if(createTime.getTime() - date.getTime()>0){
                var begin_time = {};
                begin_time.label = '上午';
                begin_time.value  = '9:00-10:00';
                begin_times.push(begin_time);
                var begin_time = {};
                begin_time.label = '下午';
                begin_time.value  = '13:00-14:00';
                begin_times.push(begin_time);
                weui.picker(begin_times, {
                    defaultValue: ['9:00-10:00'],
                    onConfirm: function(result) {
                        var time = result[0].value;
                        location.href = '${ctx}/weixin/engineer/acceptRepaire/'+id+"?time="+time;
                    }
                });
            }else { // 早于17::30  半小时工作量
                for(var i=9;i<=17;i+=0.5){
                    var time = {};
                    if((i+'').endWith('.5')){
                        time.label = (i-0.5)+':30  -  '+(i+0.5)+':00';
                        time.value  = (i-0.5)+':30  -  '+(i+0.5)+':00';
                    }else{
                        time.label = i+':00  -  '+i+':30';
                        time.value  = i+':00  -  '+i+':30';
                    }
                    begin_times.push(time);
                }
                var minutes=createTime.getMinutes()>30?"30":"00";
                var hours=createTime.getHours();
                var defaultHours= hours+":"+minutes+"  -  "+(minutes=="00"?hours+":30":(hours+1)+":00");
                weui.picker(begin_times, {
                    defaultValue: [defaultHours],
//                    onChange: function (result) {
//                        result[0].label=result[2].label;
//                        result[0].value=result[2].label;
//                        $(".weui-picker__content").eq(2).attr("style",$(".weui-picker__content").eq(0).attr("style"));
//                        console.log($(".weui-picker__content").eq(0).attr("style"));
//                    },
                    onConfirm: function(result) {
//                        var time = result[0].label + '-' + result[2].label;
//                    var expect_date = date + ' ' + time;
//                    $(_this).find('.weui-cell__ft').text(expect_date);
                        location.href = '${ctx}/weixin/engineer/acceptRepaire/'+id+"?time="+result[0].label;
                    }
                });
            }
        }

    }
    var checkSubmitFlg = true;
    // 开始服务
    function beginServer(id) {
        if (checkSubmitFlg == true) {
            checkSubmitFlg = false;
            location.href = '${ctx}/weixin/engineer/beginServer/'+id;
        }
    }
    // 结束服务
    function endServer(id) {
        if (checkSubmitFlg == true) {
            checkSubmitFlg = false;
            location.href = '${ctx}/weixin/engineer/endServer/'+id;
        }
    }

    function allocationEngineer(id){
        if (checkSubmitFlg == true) {
            checkSubmitFlg = false;
            getEngineer(id,'${ctx}/weixin/engineer/list');
        }
    }

    // 选择工程师派单
    function renderSelect(data,id,url) {
        weui.picker(data, {
            onChange: function (result) {
            },
            onConfirm: function (result) {
                var currentId = result[0].value;
                var currentName = result[0].label;
                var json = {"id":id,"currentId":currentId,"currentName":currentName};
                update(json,function () {
                    showSuccessToast('派单成功');
                    location.href= url;

                })
            }
        });
    }

    String.prototype.endWith=function(str){
        if(str==null||str==""||this.length==0||str.length>this.length)
            return false;
        if(this.substring(this.length-str.length)==str)
            return true;
        else
            return false;
        return true;
    }

    String.prototype.startWith=function(str){
        if(str==null||str==""||this.length==0||str.length>this.length)
            return false;
        if(this.substr(0,str.length)==str)
            return true;
        else
            return false;
        return true;
    }
</script>

</body>
</html>
